<?php
/**
 * 用户中心
 * 我关注的股票
 * 我收藏的话题
 * 
 */
namespace V1\Controller;
use V1\Logic\StockLogic;

class CenterController extends BaseController {
    
    protected $errorCode = array(
        'follow' => array(
             '-1'=>'用户token失效或有误',
            '-10'=>'数据没了',
        ) ,
        'bookmark' => array(
             '-1'=>'用户token失效或有误',
            '-10'=>'数据没了',
        ) ,
        'game' => array(
             '-1'=>'用户token失效或有误',
            '-10'=>'数据没了',
        ),
        'wealth' => array(
            '-1'=>'用户token失效或有误',
            '-10'=>'该用户被禁用',
        ),
    );
  
    /**
     * 我关注股票的股票列表
     */
    public function follow() {
        $data = array("code" => 0,"msg" => "","info"=>array());
        
        list($uid,$user_token) = $this->checkUserToken();
        
        $stockList = $this->page('UserStock',['stock_code'], ['user_id'=>$uid], 'id desc',120);
        $stockInfoList =[];
        foreach ($stockList as $key => $value) {
             $stockRealInfo = StockLogic::getStockInfo($this->day, $value['stock_code']);
             $stockInfoList[]= $stockRealInfo;
        }
        
        $data['info']=$stockInfoList;
        $this->responseResult($data);
    }
    /**
     * 我的竞猜
     */
    public function game(){
        
      $data = array("code" => 0,"msg" => "","info"=>array());
      
      $selectedGameType = I('game_type','gul');
      $selectedPeriodIndex=I('periodIndex',0);
      
      
      list($uid,$user_token) = $this->checkUserToken();

      for($i=0;$i<3;$i++){
          $currPeriodIndex =$selectedPeriodIndex-$i;
          $data['info']['periods'][] = date('Ym',strtotime("{$currPeriodIndex}month"));
      }
      $data['info']['periodIndex'] = $selectedPeriodIndex-3;
      
      $selectedPeriod= I('period',$data['info']['periods'][0]);
  
      $gameModel = D('UserGameView');
     
      $where = array(
          'user_id' => $uid,
          'status' => array('in',array(1,0)),
          'game_type'=>$selectedGameType,
          'period'=> $selectedPeriod,
       );
      
       $userGamelist =$gameModel->where($where)->order('id desc')->select();
      
       empty($userGamelist) && $this->responseError(-10);
      
       foreach ($userGamelist as $key => $value ) {
           $userGamelist[$key]['day_time']= date('d',$value['create_time']);
       }
      
      $data['info']['summary']=$gameModel->getGameSummary($uid,$selectedGameType);
      $data['info']['list'] = $userGamelist;
      $this->responseResult($data);
      
    }
    /**
     * 我的财富
     */
    public function wealth(){
     
      $data = array("code" => 0,"msg" => "","info"=>array());
      
      list($uid,$user_token) = $this->checkUserToken();
      
      $userInfo = $this->find('User', array('id'=>$uid,'status'=>1));
      empty($userInfo) && $this->responseError(-10);
   
      $detail_action=I('v','');
      $detail_action = ucfirst($detail_action);
      if(!empty($detail_action) && in_array($detail_action, ['Experience','Money'])){
        $selectedPeriodIndex=I('periodIndex',0);
        for($i=0;$i<3;$i++){
            $currPeriodIndex =$selectedPeriodIndex-$i;
            $data['info']['periods'][] = date('Ym',strtotime("{$currPeriodIndex}month"));
        }
        $data['info']['periodIndex'] = $selectedPeriodIndex-3;

        $selectedPeriod= I('period',$data['info']['periods'][0]);
        $LogModel = M("User{$detail_action}Log");

        $where = array(
            'user_id' => $uid,
            'status' => 1,
            'period'=> $selectedPeriod,
         );

        
        $vlist= $LogModel->where($where)->select();
        $data['info']['list']=[];
        if(!empty($vlist) ){
             foreach ($vlist as $key => $value ) {
                $vlist[$key]['day_time']= date('d',$value['create_time']);
            }
        $data['info']['list']=$vlist;
      } 
        $this->responseResult($data);
      }
     
      $data['info']['money'] = $userInfo[0]['money'];
      $data['info']['experience'] = $userInfo[0]['experience'];
      $this->responseResult($data);
    }
    /**
     * 我收藏的话题列表
     */
    public function bookmark() {
      
      $data = array("code" => 0,"msg" => "","info"=>array());
      
      list($uid,$user_token) = $this->checkUserToken();
      
      $pageSize = I('r',10,'intval');
      $pageInde = I('p',1,'intval');
      
      $model = D('TopicView');
      $list =$model->where(array('user_id' => $uid))->order('update_time desc')->page($pageInde,$pageSize)->select();
      
      empty($list) && $this->responseError(-10);
    
      $data['info'] = $list;
      $this->responseResult($data);
    }
    
}